package ar.edu.caece.compiler.grammar.factories;

import ar.edu.caece.compiler.grammar.AbstractTokenVisitorFactory;
import ar.edu.caece.compiler.grammar.GrammarSymbol;
import ar.edu.caece.compiler.grammar.TokenVisitorInterface;
import ar.edu.caece.compiler.grammar.nonterminals.EXPR2;
import ar.edu.caece.compiler.grammar.nonterminals.EXPRA;
import ar.edu.caece.compiler.grammar.nonterminals.EXPRA1;
import ar.edu.caece.compiler.grammar.nonterminals.OP1;
import ar.edu.caece.compiler.grammar.terminals.GrammarTerminal;

public class EXPRA1Factory extends AbstractTokenVisitorFactory {

	private static EXPRA1Factory INSTANCE;
	
	private OP1Factory op1Factory;
	private EXPR2Factory expr2Factory;
	private EXPRAFactory expraFactory;
	private TokenVisitorInterface parseOP1;
	private TokenVisitorInterface parseEXPR2;
	private TokenVisitorInterface parseEXPRA;
	
	public static EXPRA1Factory getInstance() throws Exception {
		if(INSTANCE==null) INSTANCE = new EXPRA1Factory();
		return INSTANCE;
	}
	
	private Initializer initializer = new Initializer() {
		
		@Override
		public void init() throws Exception {
			initializer = NoOperationInitializer.getInstance();
			op1Factory = OP1Factory.getInstance();
			expr2Factory = EXPR2Factory.getInstance();
			expraFactory = EXPRAFactory.getInstance();
			parseEXPRA  = expraFactory.makeVisitor();
			parseEXPR2 = expr2Factory.makeVisitor();
			parseOP1 = op1Factory.makeVisitor();
		}
	};
	
	private EXPRA1Factory() throws Exception {

	}
	
	@Override
	public TokenVisitorInterface makeChainedVisitor(
			final TokenVisitorInterface siguiente) throws Exception {
		initializer.init();

		return new TokenVisitorInterface() {

			@Override
			public GrammarSymbol defaultCase(GrammarTerminal llamador)
					throws Exception {
				GrammarSymbol result = llamador.execute(op1Factory.makeChainedVisitor(siguiente));
				if(result instanceof OP1) {
					return new EXPRA1 ((OP1) result, (EXPR2) nextToken().execute(parseEXPR2),(EXPRA) nextToken().execute(parseEXPRA)); 
				} 
				else {
					return result;
				}
			}
			
		};
	
	}
	@Override
	public TokenVisitorInterface makeVisitor() throws Exception {
		initializer.init();

		return new TokenVisitorInterface() {

			@Override
			public GrammarSymbol defaultCase(GrammarTerminal llamador) throws Exception {
				return new EXPRA1 ((OP1) llamador.execute(parseOP1), (EXPR2) nextToken().execute(parseEXPR2),(EXPRA) nextToken().execute(parseEXPRA));
			}
		};
	}
	
}
